<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet [
  <!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#">
  <!ENTITY dc "http://purl.org/dc/elements/1.1/">
  <!ENTITY dcterms "http://purl.org/dc/terms/">
  <!ENTITY foaf "http://xmlns.com/foaf/0.1/">
  <!ENTITY a "http://xmlns.heustics.org/schema/myelin/0.1/">
  <!ENTITY m "http://xmlns.heustics.org/ontology/myelin/0.1/">
]>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  xmlns:l="http://xmlns.heustics.org/schema/i18n/0.1/"
  xmlns:a="http://xmlns.heustics.org/schema/myelin/0.1/" 
  xmlns:fhttp="http://xmlns.4suite.org/4ss/http"
  xmlns:email="http://xmlns.4suite.org/4ss/modules/e-mail"
  xmlns:xf="http://www.w3.org/2002/xforms"
  xmlns:o="http://xmlns.heustics.org/schema/myelin/0.1/UserAccount#join"
  xmlns:n="http://xmlns.heustics.org/schema/myelin/0.1/UserAccount#new"
  xmlns:str="http://exslt.org/strings"
  extension-element-prefixes="fhttp email str"
  exclude-result-prefixes="xsl a l o fhttp email xf str"
>  
  <xsl:variable name="o:l10n" select="a:get-l10n('&a;UserAccount#join')"/>
  <xsl:variable name="o:titles-l10n" select="$o:l10n/l:context[@name='titles']/*"/> 
  <xsl:variable name="o:params-l10n" select="$o:l10n/l:context[@name='params']/*"/>
  <xsl:variable name="o:hints-l10n" select="$o:l10n/l:context[@name='params-hints']/*"/>
  
  <xsl:template match="a:title[@key='join']" mode="a:head-content">
    <xsl:call-template name="a:head-title">
      <xsl:with-param name="title" select="$o:titles-l10n[1]"/>
    </xsl:call-template>
  </xsl:template>
  
  <xsl:template match="a:title[@key='join']" mode="a:page-header">
    <xsl:call-template name="a:page-header-title">
      <xsl:with-param name="title" select="$o:titles-l10n[1]"/>
    </xsl:call-template>
  </xsl:template>   
  
  <xsl:template match="a:form[@mode='main' and @status='commit-success']">
    <xsl:param name="config" select="/*/a:config"/>
    <xsl:variable name="email-l10n" select="$o:l10n/l:context[@name='email-activate']/*"/>
    <xsl:variable name="node-url" select="a:public-url-full-secure(a:commit-node/@node-uri)"/>  
    <xsl:variable name="inst" select="xf:model/xf:instance/*"/>
    <xsl:variable name="agent" select="$inst/a:agent"/>
    <xsl:variable name="domain" select="$inst/a:memberships/a:domain"/>
    <email:send-mail 
      to="{$agent/a:name} &lt;{substring($agent/a:mbox/@uri,8)}&gt;"
      from="{$config/@email-from}"
      subject="{$email-l10n[1]}"
      method="html"
      media-type="text/html"
      server="{$config/@smtp-gateway}">          
      <html>
        <body>        
          <xsl:apply-templates select="$inst" mode='a:activate-email-body'>          
            <xsl:with-param name="email-l10n" select="$email-l10n"/>
            <xsl:with-param name="node-url" select="$node-url"/>    
            <xsl:with-param name="activation-key" select="a:commit-node/@activation-key"/>      
            <xsl:with-param name="agent" select="$agent"/>
            <xsl:with-param name="domain" select="$domain"/>
          </xsl:apply-templates>
        </body>
      </html>     
    </email:send-mail>    
    <xsl:choose>
      <xsl:when test="$domain/a:options/@count &gt; 1">
        <fhttp:response-uri
          uri='{a:format-url-full-secure(
            a:encode-url(
              a:format-url-target-action(
                a:format-url-target(
                  a:format-repeat-request(
                    "activate"
                  ),
                  @target
                ),
                @target-action
              ),
              a:encode-url-param-chain("msg","new-activate",
                a:encode-url-param-chain("domain",$domain/@selected,
                  a:encode-url-param("nick",string($agent/a:nick))
                )
              )
            )
          )}'/>
      </xsl:when>
      <xsl:otherwise>
        <fhttp:response-uri
          uri='{a:format-url-full-secure(
            a:encode-url(
              a:format-url-target-action(
                a:format-url-target(
                  a:format-repeat-request(
                    "activate"
                  ),
                  @target
                ),
                @target-action
              ),
              a:encode-url-param-chain("msg","new-activate",
                a:encode-url-param("nick",string($agent/a:nick))
              )
            )
        )}'/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template> 
  
  <xsl:template match="a:*" mode="a:activate-email-body">   
    <xsl:param name="email-l10n"/>      
    <xsl:param name="node-url"/>
    <xsl:param name="activation-key"/>
    <xsl:param name="agent" select="a:agent"/>
    <xsl:param name="domain" select="a:memberships/a:domain"/>
    <p><xsl:value-of select="concat($email-l10n[2],$agent/a:name)"/></p>
    <p><xsl:value-of select="$email-l10n[3]"/></p>
    <ul>
      <li><xsl:value-of select="concat($email-l10n[4],$agent/a:nick)"/></li>
      <li><xsl:value-of select="concat($email-l10n[5],$activation-key)"/></li>
      <li><xsl:value-of select="concat($email-l10n[6],$agent/a:password)"/></li>
      <xsl:if test="$domain/a:options/@count &gt; 1">
        <li><xsl:value-of select="concat($email-l10n[7],$domain/@label)"/></li>
      </xsl:if>
    </ul>
    <p>
      <xsl:value-of select="$email-l10n[8]"/>
    </p>
    <p>
      <xsl:variable name="link" 
        select="a:format-url-ext(
          a:format-url-ext(
            a:format-url-ext(
              a:format-url-ext(
                a:format-url-min($node-url,'activate'),
                'commit','commit'
              ),
              'event','commit'
            ),
            'activation-key',$activation-key
          ),
          'password',$agent/a:password
        )"/>
      <a href='{$link}'>
        <xsl:value-of select="$link"/>
      </a>
    </p>
    <p>
      <xsl:value-of select="$email-l10n[9]"/>
    </p>        
    <p>
      <xsl:variable name="link" 
        select="a:format-url-min($node-url,'activate')"/>   
      <a href='{$link}'>     
        <xsl:value-of select="$link"/>
      </a>
    </p>
  </xsl:template>
  
</xsl:stylesheet>

